starter.controller('busMapInfoCtrl', ['ApiEndpoint','$rootScope','$window','$state','$ionicHistory','$timeout','$ionicScrollDelegate','$ionicPopup', '$scope', 'Page',
    function(ApiEndpoint,$rootScope,$window,$state,$ionicHistory,$timeout,$ionicScrollDelegate,$ionicPopup, $scope, Page) {


        var transParams = $state.params.transParams;
        if(transParams == undefined || jQuery.isEmptyObject(transParams)) {
            $state.go('bus',{},{reload : true});
            return;
        }

        $scope.screenWidth = document.body.clientWidth;
        $scope.screenHeight = window.innerHeight;

        var mapDiv = "busLineMapDiv";
        var width = $('#' + mapDiv).width();
        var height = $('#' + mapDiv).height();


        var map = new AMap.Map("busLineMapDiv", {
            resizeEnable: true,
            zoom : 13,
            center: [114.296308, 30.595444]
        });
        //map.setZoom(12);

        var loadi = $ionicPopup.alert({
            cssClass : 'popup_notice',
            template: '正在定位...',
            buttons:null
        });
        $timeout(function(){
            loadi.close();
        },2000);

        Page.ajaxPostQuiet(ApiEndpoint.gpsUrl, {vehicleNo : transParams.vehicleNo,lineNo : transParams.lineNo}, function (resp) {
            if(resp.data && resp.data.gpsdatatime){

                var po = AppUtil.GPS.gcj_encrypt(resp.data.latitude*1, resp.data.longitude*1);
                var infoWindow = new AMap.InfoWindow({
                    content:transParams.vehicleNo + '【运行线路：' + resp.data.lineName + '】【最后定位时间：' + resp.data.gpsdatatime +'】【方向角：' + resp.data.direction + '】'
                });
                var busMarker = new AMap.Marker({
                    position: [po.lon, po.lat],
                    //icon: "img/icon/map_bus_orange.png",
                    icon : new AMap.Icon({imageSize:new AMap.Size(20,20),image:'img/icon/map_bus_orange.png'}),
                    offset: new AMap.Pixel(-14.5, -12),
                    map:map
                }).on('click',function(e){
                        infoWindow.open(map, e.target.getPosition());
                    });
                infoWindow.open(map, busMarker.getPosition());
            } else {
                var popup2 = $ionicPopup.alert({
                    cssClass : 'popup_notice',
                    template: '定位失败...',
                    buttons:null
                });
                $timeout(function(){
                    popup2.close();
                },1000);
            }
        });

        if(transParams.lineNo){
            AMap.service(["AMap.LineSearch"], function() {
                var linesearch = new AMap.LineSearch({
                    city:'027',
                    extensions:'all'
                });
                linesearch.search(transParams.lineNo, function(status, result) {
                    if (status === 'complete' && result.info === 'OK' && result.lineInfo.length > 0) {
                        var busArr = result.lineInfo[0].path;
                        var stops = result.lineInfo[0].via_stops;
                        var startPot = stops[0].location;
                        var endPot = stops[stops.length - 1].location;

                        new AMap.Marker({
                            map: map,
                            position: [startPot.lng, startPot.lat], //基点位置
                            icon: "http://webapi.amap.com/theme/v1.3/markers/n/start.png",
                            zIndex: 10
                        });
                        new AMap.Marker({
                            map: map,
                            position: [endPot.lng, endPot.lat], //基点位置
                            icon: "http://webapi.amap.com/theme/v1.3/markers/n/end.png",
                            zIndex: 10
                        });
                        //绘制乘车的路线
                        busPolyline = new AMap.Polyline({
                            map: map,
                            path: busArr,
                            strokeColor: "#09f",//线颜色
                            strokeOpacity: 0.8,//线透明度
                            strokeWeight: 5//线宽
                        });
                    }
                        map.setFitView();
                });
            });
        }

        //$timeout(function() {
        //    map.setFitView();
        //},300);


        /*公交线路查询*/
        //function lineSearch() {
        //    AMap.service('AMap.LineSearch',function(){//回调函数
        //        //实例化公交线路查询类，只取回一条路线
        //        var linesearch = new AMap.LineSearch({
        //            pageIndex: 1,
        //            city: '武汉',
        //            pageSize: 1,
        //            extensions: 'all'
        //        });
        //        //搜索“536”相关公交线路
        //        linesearch.search(transParams.lineNo, function(status, result) {
        //            if (status === 'complete' && result.info === 'OK') {
        //                lineSearch_Callback(result);
        //            } else {
        //                //alert(result);
        //            }
        //        });
        //    })
        //}
        ///*公交路线查询服务返回数据解析概况*/
        //function lineSearch_Callback(data) {
        //    var lineArr = data.lineInfo;
        //    var lineNum = data.lineInfo.length;
        //    if (lineNum == 0) {
        //
        //    } else {
        //        for (var i = 0; i < lineNum; i++) {
        //            var pathArr = lineArr[0].path;
        //            var stops = lineArr[0].via_stops;
        //            //var startPot = stops[0].location;
        //            //var endPot = stops[stops.length - 1].location;
        //            for (var j = 0; j < stops.length; j++) {
        //                if(j == 0) {
        //                    drawbusLine('start',stops[j], pathArr);
        //                } else if(j == stops.length - 1) {
        //                    drawbusLine('end',stops[j], pathArr);
        //                } else {
        //                    drawbusLine('stop',stops[j], pathArr);
        //                }
        //            }
        //
        //        }
        //    }
        //}
        ///*绘制路线*/
        //function drawbusLine(flag, stopInfo, BusArr) {
        //    var marker;
        //    if(flag == "start") {
        //        //绘制起点
        //        marker = new AMap.Marker({
        //            map: map,
        //            position: [stopInfo.location.lng, stopInfo.location.lat], //基点位置
        //            icon: "http://webapi.amap.com/theme/v1.3/markers/n/start.png",
        //            zIndex: 10,
        //            title: stopInfo.name
        //        });
        //    } else if(flag == "end") {
        //        //绘制终点
        //        marker = new AMap.Marker({
        //            map: map,
        //            position: [stopInfo.location.lng, stopInfo.location.lat], //基点位置
        //            icon: "http://webapi.amap.com/theme/v1.3/markers/n/end.png",
        //            zIndex: 10,
        //            title: stopInfo.name
        //        });
        //    } else {
        //        marker = new AMap.Marker({
        //            map: map,
        //            position: [stopInfo.location.lng, stopInfo.location.lat], //基点位置
        //            icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png",
        //            zIndex: 10,
        //            title: stopInfo.name
        //        });
        //    }
        //
        //    marker.info = new AMap.InfoWindow({
        //        content: stopInfo.name,
        //        offset: new AMap.Pixel(0, -30)
        //    });
        //    marker.on('click', function(e) {
        //        e.target.info.open(map, e.target.getPosition())
        //    })
        //
        //    if(flag == "end") {
        //        //绘制乘车的路线
        //        busPolyline = new AMap.Polyline({
        //            map: map,
        //            path: BusArr,
        //            strokeColor: "#09f",//线颜色
        //            strokeOpacity: 0.8,//线透明度
        //            strokeWeight: 6//线宽
        //        });
        //
        //        map.setFitView();
        //    }
        //
        //}
        //lineSearch();

        //$scope.busLineMap = new RMap(document.getElementById(mapDiv), 114.296308, 30.595444, 8, width, height);
        //$scope.busLineMap.show();

        //var map = new AMap.Map('busLineMapDiv', {
        //    resizeEnable: true,
        //    zoom:11,
        //    center: [114.296308, 30.595444]
        //
        //});

        /*
         * 该示例主要流程分为三个步骤
         * 1. 首先调用公交路线查询服务(lineSearch)
         * 2. 根据返回结果解析，输出解析结果(lineSearch_Callback)
         * 3. 在地图上绘制公交线路()
         */

        //if(typeof AMap.Map != 'function') {
        //    var completePopup = $ionicPopup.alert({
        //        title: '警告',
        //        template: '地图控件加载失败!'
        //    });
        //    $state.go('bus',null,{reload : true});
        //    $timeout(function() {
        //        completePopup.close(); // 1秒后关闭弹窗
        //    }, 1500);
        //    return;
        //}



    }]);
